﻿using GalaSoft.MvvmLight;

namespace Budgetsch.WP7.ViewModel
{
    using System.Windows;

    using Budgetsch.WP7.Model.DataService;
    using Budgetsch.WP7.Navigation;
    using Budgetsch.WP7.ViewModel.Cost;
    using Budgetsch.WP7.ViewModel.Income;
    using Budgetsch.WP7.ViewModel.Saving;

    using GalaSoft.MvvmLight.Ioc;
    using GalaSoft.MvvmLight.Messaging;

    public class CommentWriterViewModel : ViewModelBase
    {
        private readonly ICommentDataService _commentDataService;

        private readonly INavigationService _navigationService;

        /// <summary>
        /// The <see cref="UserName" /> property's name.
        /// </summary>
        public const string UserNamePropertyName = "UserName";

        private string _userName;

        /// <summary>
        /// Sets and gets the UserName property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string UserName
        {
            get
            {
                return _userName;
            }

            set
            {
                if (_userName == value)
                {
                    return;
                }

                _userName = value;
                RaisePropertyChanged(UserNamePropertyName);
            }
        }

        /// <summary>
        /// The <see cref="IsBusy" /> property's name.
        /// </summary>
        public const string IsBusyPropertyName = "IsBusy";

        private bool _isBusy = false;

        /// <summary>
        /// Sets and gets the IsBusy property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public bool IsBusy
        {
            get
            {
                return _isBusy;
            }

            set
            {
                if (_isBusy == value)
                {
                    return;
                }

                _isBusy = value;
                RaisePropertyChanged(IsBusyPropertyName);
            }
        }

        /// <summary>
        /// Initializes a new instance of the CommentWriterViewModel class.
        /// </summary>
        public CommentWriterViewModel(ICommentDataService commentDataService, INavigationService navigationService)
        {
            _commentDataService = commentDataService;
            _navigationService = navigationService;

            Messenger.Default.Register<AddToUserNameMessage>(this, m => this.UserName = m.UserName);
            Messenger.Default.Register<CreateNewIncomeCommentMessage>(
                this,
                m =>
                {
                    IsBusy = true;
                    this._commentDataService.SendIncomeComment(
                        m.Message,
                        m.ItemId,
                        (comment, exp) =>
                        {
                            IsBusy = false;
                            if (exp != null || comment == null)
                            {
                                MessageBox.Show("Can't send the comment!", "Error", MessageBoxButton.OK);
                            }
                            else
                            {
                                SimpleIoc.Default.GetInstance<IncomeDetailsViewModel>();
                                Messenger.Default.Send(
                                    new RefreshIncomeDetailsViewMessage { Comment = comment });
                                _navigationService.GoBack();
                            }
                        });
                });
            Messenger.Default.Register<CreateNewCostCommentMessage>(this, m =>
            {
                IsBusy = true;
                this._commentDataService.SendCostComment(
                    m.Message,
                    m.ItemId,
                    (comment, exp) =>
                    {
                        IsBusy = false;
                        if (exp != null || comment==null)
                        {
                            MessageBox.Show("Can't send the comment!", "Error", MessageBoxButton.OK);
                        }
                        else
                        {
                            SimpleIoc.Default.GetInstance<CostDetailsViewModel>();
                            Messenger.Default.Send(new RefreshCostDetailsViewMessage { Comment = comment });
                            _navigationService.GoBack();
                        }
                    });
            });

            Messenger.Default.Register<CreateNewSavingCommentMessage>(this, m =>
            {
                IsBusy = true;
                this._commentDataService.SendSavingComment(
                    m.Message,
                    m.ItemId,
                    (comment, exp) =>
                    {
                        IsBusy = false;
                        if (exp != null || comment == null)
                        {
                            MessageBox.Show("Can't send the comment!", "Error", MessageBoxButton.OK);
                        }
                        else
                        {
                            SimpleIoc.Default.GetInstance<SavingDetailsViewModel>();
                            Messenger.Default.Send(new RefreshSavingDetailsViewMessage { Comment = comment });
                            _navigationService.GoBack();
                        }
                    });
            });
        }
    }
}